package com.wjy.common.nacos;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.Properties;

/**
 * @author wjy
 * @desc Nacos服务
 * @date 2019/8/11
 */
@Component
public class NacosService {

    /**
     * @author wjy
     * @desc 注册服务
     * @date 2019/8/11
     */
    public void registerService() {

        try {

            String ip = InetAddress.getLocalHost().getHostAddress();

            NamingService namingService = NamingFactory.createNamingService("127.0.0.1:8848");
            namingService.registerInstance("another", "all", ip, 8082);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @author wjy
     * @desc 注册配置
     * @date 2019/8/11
     */
    public void registerConfig() {

        try {

            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1");

            ConfigService configService = NacosFactory.createConfigService(properties);

            configService.getConfig("token.yml", "token", 5000);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
